package com.estronger.shopmanage.utils;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.MyApplication;

public class UiUtils {

	public static String[] getStringArray(int tabNames) {
		return getResource().getStringArray(tabNames);
	}

	public static Resources getResource() {
		return MyApplication.getApplication().getResources();
	}

	// 获取ApplicationContext对象
	public static Context getContext() {
		return MyApplication.getApplication();
	}

	/**
	 * 将Runnable放在ui线程执行
	 * 
	 * @param runnable
	 */
	public static void runOnUiThread(Runnable runnable) {
		// 当前线程是不是ui线程
		if (android.os.Process.myTid() == MyApplication.getMainTid()) {
			runnable.run();
		} else {
			MyApplication.getHandler().post(runnable);
		}
	}

	public static Drawable getDrawalbe(int id) {
		return getResource().getDrawable(id);
	}

	public static int getDimens(int homePictureHeight) {
		return (int) getResource().getDimension(homePictureHeight);
	}

	/**
	 * @param run
	 *            要执行的任务
	 * @param time
	 *            延迟时间
	 */
	public static void postDelayed(Runnable run, int time) {
		MyApplication.getHandler().postDelayed(run, time);
	}

	/**
	 * @param auToRunTask
	 */
	public static void cancel(Runnable auToRunTask) {
		MyApplication.getHandler().removeCallbacks(auToRunTask);
	}

	public static void startActivity(Intent intent) {
		// 不在activity里面启动activity时，需要指定Intent.FLAG_ACTIVITY_NEW_TASK
		if (BaseActivity.activity == null) {
			intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			getContext().startActivity(intent);
		} else {
			BaseActivity.activity.startActivity(intent);
		}
	}

}
